export function getTimer(t, f) {
  if (!t) {
    return "-";
  }
  var d1 = new Date(t);
  var d2 = new Date().getTime();
  let millisecond = parseInt(d2 - d1); //两个时间相差的毫秒数
  let second = parseInt(millisecond / 1000); //两个时间相差的秒数
  let minute = parseInt(second / 60); //两个时间相差的分钟数
  let hours = parseInt(minute / 60); //两个时间相差的小时数
  let days = parseInt(hours / 24); //两个时间相差的天数
  // //  console.log(millisecond,second,minute,hours,days,minute < 60 ? minute + '分钟' : hours < 24 ? hours + '小时' : days + '天');
  if (!!f) {
    return f === "d"
      ? days + "天"
      : f === "h"
      ? hours + "小时"
      : f === "m"
      ? minute + "分钟"
      : "";
  } else {
    return minute < 60
      ? minute + "分钟"
      : hours < 24
      ? hours + "小时"
      : days + "天";
  }
}

// 某一天前后几天的时间

export function BeforeOrAfterToDate(dateTime, dateLenght, timeType) {
  if (!dateTime) {
    return {
      date: "",
      year: "",
      month: "",
      day: "",
      hour: "",
      minute: "",
      second: "",
    };

    return false;
  } else if (dateLenght == undefined) {
    dateLenght = 0;
  }

  // dateTime为起点时间
  // dateLenght 为相距天数 起点时间之前为负数之后为正数 若dateTime=2021-12-02,dateLenght=-1 及最终时间为 2021-12-01
  // timeType为输出的时间格式

  // //  console.log(dateTime);
  var start = new Date(dateTime);
  var startTime = start.getTime();
  var lengthTime = dateLenght * 24 * 60 * 60 * 1000;
  var returnTime = Math.abs(new Date(startTime + lengthTime));

  var date = new Date(returnTime);

  //获取年
  var Y = date.getFullYear();
  //获取月
  var M =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;
  //获取当日
  var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

  // 当前小时
  var h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  // 当前分钟
  var m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  // 当前秒
  var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

  if (timeType === "YYYY-MM-DD") {
    return {
      date: Y + "-" + M + "-" + D,
      year: Y,
      month: M,
      day: D,
    };
  } else {
    return {
      date: Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s,
      year: Y,
      month: M,
      day: D,
      hour: h,
      minute: m,
      second: s,
    };
  }
}

// 获取当前日期
export function dayDataTime(timeType) {
  var objdate = new Date();
  var year = objdate.getFullYear();
  var getmoth =
    objdate.getMonth() + 1 <= 9
      ? "0" + (objdate.getMonth() + 1)
      : objdate.getMonth() + 1;
  var getsday =
    objdate.getDate() <= 9 ? "0" + objdate.getDate() : objdate.getDate();
  var h =
    objdate.getHours() < 10 ? "0" + objdate.getHours() : objdate.getHours();
  var m =
    objdate.getMinutes() < 10
      ? "0" + objdate.getMinutes()
      : objdate.getMinutes();

  var s =
    objdate.getSeconds() < 10
      ? "0" + objdate.getSeconds()
      : objdate.getSeconds();

  if (timeType === "YYYY-MM-DD") {
    return year + "-" + getmoth + "-" + getsday;
  } else if (timeType === "obj") {
    return {
      str: year + "-" + getmoth + "-" + getsday,
      y: year,
      m: getmoth,
      d: getsday,
    };
  } else {
    return year + "-" + getmoth + "-" + getsday + " " + h + ":" + m + ":" + s;
  }
}

export function getBeginAndEbdMonth(start, end) {
  let result = [];
  let starts = start.split("-");
  let ends = end.split("-");
  let staYear = parseInt(starts[0]);
  let staMon = parseInt(starts[1]);
  let endYear = parseInt(ends[0]);
  let endMon = parseInt(ends[1]);
  while (staYear <= endYear) {
    if (staYear === endYear) {
      while (staMon < endMon) {
        staMon++;
        let str = staYear + "-" + (staMon >= 10 ? staMon : "0" + staMon);
        result.push(str);
      }
      staYear++;
    } else {
      staMon++;
      if (staMon > 12) {
        staMon = 1;
        staYear++;
      }
      let str = staYear + "-" + (staMon >= 10 ? staMon : "0" + staMon);
      result.push(str);
    }
  }
  // //  console.log(result);
  return result;
}

export function monthDays(yyyymm) {
  let ym = [];
  if (!yyyymm) {
    return false;
  } else if (yyyymm.split("-").length > 0) {
    ym = yyyymm.split("-");
  } else if (yyyymm.split("/").length > 0) {
    ym = yyyymm.split("/");
  }

  let year = ym[0];
  let month = ym[1];

  let days_list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

  //判断是否为闰年
  if (month == 2 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))) {
    return days_list[month - 1] + 1;
  } else {
    return days_list[month - 1];
  }
}

export function capitalAmount(amount) {
  // 汉字的数字
  const cnNums = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
  // 基本单位
  const cnIntRadice = ["", "拾", "佰", "仟"];
  // 对应整数部分扩展单位
  const cnIntUnits = ["", "万", "亿", "兆"];
  // 对应小数部分单位
  const cnDecUnits = ["角", "分", "毫", "厘"];
  // 整数金额时后面跟的字符
  const cnInteger = "整";
  // 整型完以后的单位
  const cnIntLast = "元";
  // 最大处理的数字
  const maxNum = 9999999999999999.99;
  // 金额整数部分
  let integerNum;
  // 金额小数部分
  let decimalNum;
  // 输出的中文金额字符串
  let chineseStr = "";
  // 分离金额后用的数组，预定义
  let parts;
  if (amount === "") {
    return "";
  }
  amount = parseFloat(amount);
  if (amount >= maxNum) {
    // 超出最大处理数字
    return "";
  }
  if (amount === 0) {
    chineseStr = cnNums[0] + cnIntLast + cnInteger;
    return chineseStr;
  }
  // 转换为字符串
  amount = amount.toString();
  if (amount.indexOf(".") === -1) {
    integerNum = amount;
    decimalNum = "";
  } else {
    parts = amount.split(".");
    integerNum = parts[0];
    decimalNum = parts[1].substr(0, 4);
  }
  // 获取整型部分转换
  if (parseInt(integerNum, 10) > 0) {
    let zeroCount = 0;
    const IntLen = integerNum.length;
    for (let i = 0; i < IntLen; i++) {
      const n = integerNum.substr(i, 1);
      const p = IntLen - i - 1;
      const q = p / 4;
      const m = p % 4;
      if (n === "0") {
        zeroCount++;
      } else {
        if (zeroCount > 0) {
          chineseStr += cnNums[0];
        }
        // 归零
        zeroCount = 0;
        chineseStr += cnNums[parseInt(n, 10)] + cnIntRadice[m];
      }
      if (m === 0 && zeroCount < 4) {
        chineseStr += cnIntUnits[q];
      }
    }
    chineseStr += cnIntLast;
  }
  // 小数部分
  if (decimalNum !== "") {
    const decLen = decimalNum.length;
    for (let i = 0; i < decLen; i++) {
      const n = decimalNum.substr(i, 1);
      if (n !== "0") {
        chineseStr += cnNums[Number(n)] + cnDecUnits[i];
      }
    }
  }
  if (chineseStr === "") {
    chineseStr += cnNums[0] + cnIntLast + cnInteger;
  } else if (decimalNum === "") {
    chineseStr += cnInteger;
  }
  return chineseStr;
}

export function TimeDisable(amount) {
  return {
    disabledDate(time) {
      // 获取当前日期的年月
      const currentYear = new Date().getFullYear();
      const currentMonth = new Date().getMonth() + 1;

      // 获取当前选择的年月
      const selectedYear = time.getFullYear();
      const selectedMonth = time.getMonth() + 1;

      // 如果选择的年份大于当前年份，或者选择的年份等于当前年份但月份大于当前月份，则禁用
      return (
        selectedYear > currentYear ||
        (selectedYear === currentYear && selectedMonth > currentMonth)
      );
    },
  };
}

// 本周日期

export function getMonday() {
  const now = new Date();
  const nowTime = now.getTime();
  const day = now.getDay();
  const longTime = 24 * 60 * 60 * 1000;

  const monday = new Date(nowTime - (day - 1) * longTime);
  const sunday = new Date(nowTime + (7 - day) * longTime);

  const sy = monday.getFullYear();
  const sm =
    monday.getMonth() + 1 <= 9
      ? "0" + (monday.getMonth() + 1)
      : monday.getMonth() + 1;
  const sd = monday.getDate() <= 9 ? "0" + monday.getDate() : monday.getDate();

  const ey = sunday.getFullYear();
  const em =
    sunday.getMonth() + 1 <= 9
      ? "0" + (sunday.getMonth() + 1)
      : sunday.getMonth() + 1;
  const ed = sunday.getDate() <= 9 ? "0" + sunday.getDate() : sunday.getDate();

  return {
    s: sy + "-" + sm + "-" + sd,
    e: ey + "-" + em + "-" + ed,
  };
}
